package com.sh.二叉树;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class 二叉树的层序遍历 {
    public static void main(String[] args) {

    }



    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> resList = new ArrayList<List<Integer>>();
        if (root==null){
            return resList;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root) ;
        /**
         *
         * A B E C D G
         *
         */



        while (!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int currentLength = queue.size();
            for (int i = 0; i < currentLength; i++) {
                TreeNode poll = queue.poll();
                if (poll.left!=null){
                    queue.add(poll.left);
                }
                if (poll.right!=null){
                    queue.add(poll.right);
                }
                list.add(poll.val);
            }
            resList.add(list);
        }
        return resList;
    }



}

